package com.jnu.student.fragment;

import android.os.Bundle;

import androidx.annotation.NonNull;
import androidx.fragment.app.Fragment;

import android.os.Handler;
import android.os.Looper;
import android.os.Message;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Toast;
import com.baidu.mapapi.map.BitmapDescriptor;
import com.baidu.mapapi.map.BitmapDescriptorFactory;
import com.baidu.mapapi.map.MapStatus;
import com.baidu.mapapi.map.MapStatusUpdateFactory;
import com.baidu.mapapi.map.MapView;
import com.baidu.mapapi.map.MarkerOptions;
import com.baidu.mapapi.map.OverlayOptions;
import com.baidu.mapapi.map.TextOptions;
import com.baidu.mapapi.model.LatLng;
import com.jnu.student.R;
import com.jnu.student.Shop;
import com.jnu.student.ShopLoader;

import java.util.ArrayList;

public class MapFragment extends Fragment {
    private MapView mapView;

    public MapFragment() {
        // Required empty public constructor
    }

    public void drawShops(ArrayList<Shop> shops){
        //获取标记点图标
        BitmapDescriptor bitmap = BitmapDescriptorFactory.fromResource(R.drawable.flag);

        for(int index=0;index<shops.size(); index++)
        {
            Shop shop=shops.get(index);
            LatLng centerPoint = new LatLng(shop.getLatitude(),shop.getLongitude());
            //添加位置图标
            MarkerOptions markerOption = new MarkerOptions().icon(bitmap).position(centerPoint);
            mapView.getMap().addOverlay(markerOption);

            //添加位置文字信息
            OverlayOptions textOption = new TextOptions().bgColor(0xAAFFFF00).fontSize(50)
                    .fontColor(0xFFFF00FF).text(shop.getName()).rotate(0).position(centerPoint);
            mapView.getMap().addOverlay(textOption);

            //标记被点击出现提示语
            mapView.getMap().setOnMarkerClickListener(maker -> {
                Toast.makeText(getContext(), "Marker被点击了！", Toast.LENGTH_SHORT).show();
                return false;
            });
        }
    }

    // TODO: Rename and change types and number of parameters
    public static MapFragment newInstance() {
        MapFragment fragment = new MapFragment();
        Bundle args = new Bundle();

        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View rootView = inflater.inflate(R.layout.fragment_map, container, false);
        mapView = rootView.findViewById(R.id.baidu_map_view);

        //获取地图及设置初始中心点
        LatLng centerPoint = new LatLng(22.255925,113.541112);
        MapStatus.Builder builder = new MapStatus.Builder();
        builder.zoom(18.0f).target(centerPoint);
        mapView.getMap().setMapStatus(MapStatusUpdateFactory.newMapStatus(builder.build()));

        ShopLoader shopLoader = new ShopLoader();
        Handler handler=new Handler(Looper.getMainLooper()) {
            @Override
            public void handleMessage(@NonNull Message msg) {
                super.handleMessage(msg);
                drawShops(shopLoader.getShops());
            }
        };
        shopLoader.load(handler, "http://file.nidama.net/class/mobile_develop/data/bookstore.json");

        return rootView;
    }

    @Override
    public void onResume() {
        super.onResume();
        //在activity执行onResume时执行mMapView. onResume ()，实现地图生命周期管理
        mapView.onResume();
    }

    @Override
    public void onPause() {
        super.onPause();
        //在activity执行onPause时执行mMapView. onPause ()，实现地图生命周期管理
        mapView.onPause();
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        //在activity执行onDestroy时执行mMapView.onDestroy()，实现地图生命周期管理
        mapView.onDestroy();
    }
}